package com.itheima.reggie.controller;

/*
@date 2022年07月27日14:40
*/


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.commom.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    //登入
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        String password = employee.getPassword();
        String username = employee.getUsername();
        log.info("接收到登录请求,username={},password={}", username, password);
        // 1.将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2 根据页面提交的用户名username查询数据库中员工数据信息
        //select * from employee where username='username'
        LambdaQueryWrapper<Employee> lqw = Wrappers.<Employee>lambdaQuery();
        lqw.eq(Employee::getUsername, username);
        Employee emp = employeeService.getOne(lqw);
        //3 如果没有查询到, 则返回登录失败结果
        if (emp == null) {
            return R.error("用户名或密码错误");
        }
        //4密码比对，如果不一致, 则返回登录失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("用户名或密码错误");
        }
        //5查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        //6登录成功，将员工id存入Session, 并返回登录成功结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }
    //登出
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    //新增员工代码实现
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
        log.info("新增员工,员工信息:{}",employee.toString());
        //设置初始密码,对密码进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //设置创建时间修改时间
    /*    employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //设置创建人,修改人
        long empId = (long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);*/
        //调用employeeService
        employeeService.save(employee);
        return R.success("新增员工成功!");
    }
    //分页代码实现
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("page={},pageSize={},name={}", page, pageSize, name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        //设置模糊查询条件   StringUtils-org.apache.commons.lang
        lqw.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //调用employeeService
        employeeService.page(pageInfo, lqw);
        return R.success(pageInfo);
    }
    //修改员工 启用 禁用员工账号
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());
        //不允许修改管理员操作
        Employee empManger = employeeService.getById(employee.getId());
        if("admin".equals(empManger.getUsername())){
            return  R.error("不允许修改管理员");
        }
        //设置修改的时间 人
 /*       long  empId= (long) request.getSession().getAttribute("employee");
        employee.setUpdateTime(LocalDateTime.now());
        employee.setUpdateUser(empId);*/
        employeeService.updateById(employee);
        return  R.success("员工信息修改成功");
    }
//编辑员工信息
@GetMapping("/{id}")
  public R<Employee> getId(@PathVariable long id) {
    log.info("根据id查询员工信息...");
    Employee employee = employeeService.getById(id);
    if (employee != null) {
        return R.success(employee);
    }
    return R.error("没有查询到对应员工信息");
}
}
